#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int count = 0;
int swap(char* str, int a, int b){
    int length = strlen(str);
    if ((length < a ) ||  (length < b))
        return -1;
    
    char temp = str[a];
    str[a] = str[b];
    str[b] = temp;

    return 0;
}

int fullarraged(char* str, int offset)
{
    if (*str == '\0')
    {
        printf("%s", str);
        return 0;
    }
    //remember to add offset before the string has been changed to str+offset.
    if (strlen(str + offset) == 1)
    {
        printf("%s\n", str);
        count ++ ;
        return 0;
    }   
    int begin = 0;
    //remember to add offset before the string has been changed to offset.
    while(str[ offset + begin] != '\0'){
        if (swap(str, offset+begin, offset) == -1)
        {   
            printf("\n before swap error\n");
            printf(" a %d b %d \n ", offset + begin, offset);
            printf("%s\n", str + offset);
            printf("swap error\n\n");
            break;
        }
        fullarraged(str, offset + 1);        
        //***************** remember to swap back important!!!!!*************************
        if (swap(str, offset+begin, offset) == -1)
        {   
            printf("\n after swap error\n");
            printf(" a %d b %d \n ", offset + begin, offset);
            printf("%s\n", str + offset);
            printf("swap error\n\n");
            break;
        }
        begin++ ;

    }
   
    return 0;
}

 int main(int argc, char const *argv[])
{
    /* code */
    char str[5] = "abcd";
    fullarraged(str, 0);
    printf("count %d\n", count);
    return 0;
}






/*#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct seq{
    char str[7];
};

struct seq seqs[800];
int count;

int is_swap(char *str, int begin, int k){   ///判断是否需要交换
    int i, flag;

    for (i = begin, flag = 1; i < k; i ++) {
        if (str[i] == str[k]) {
            flag = 0;
            break;
        }
    }

    return flag;
}

void swap(char *str, int a, int b)  ///交换字母
{
    char temp;
    temp = str[a];
    str[a] = str[b];
    str[b] = temp;
}

void permutation_process(char *name, int begin, int end) {
    int k;

    if (begin == end - 1)   ///递归出口
    {
        strcpy(seqs[count].str, name);  ///进行保存，用于后续的输出
        count ++;
    }
    else
    {
        for (k = begin; k < end; k ++)
        {
            if (is_swap(name, begin, k))    ///判断是否要进行交换。如果没有交换，则忽略这种情况
            {
                swap(name, k, begin);   ///字母交换
                permutation_process(name, begin + 1, end);  ///对[begin + 1,end)进行全排列
                swap(name, k, begin);   ///字母换回
            }
        }
    }
}

int compare(const void *p, const void *q)
{
    const char *a = (char*)p;
    const char *b = (char*)q;
    return strcmp(a, b);
}

int main()
{
    char name[7];   ///保存要进行全排列的字符串，长度应较小(小于7)
    int i, len;

    while (scanf("%s", name) != EOF) {
        count = 0;
        len = strlen(name);
        permutation_process(name, 0, len);  ///全排列
        qsort(seqs, count, sizeof(seqs[0]), compare);   ///将生成的全排列排序

        for (i = 0; i < count; i ++)
            printf("%s\n", seqs[i].str);
        printf("\n");
    }

    return 0;
}*/